//当作模块
import Vue from 'vue'
import VueRouter from 'vue-router'
//相对路径 .当前目录 .. 升级目录 
//import Index from '../views/index.vue'
import Index from '@/views/index.vue'
import ArticleDetail from '@/views/article/detail.vue'
import NotFound from '@/views/notfound.vue'
import ArticleOther from '@/views/article/other.vue'
import Header from '@/views/header.vue'
import Footer from '@/views/footer.vue'
import store from '../store'
Vue.use(VueRouter)
//路由表
//访问规则：从上往下，只要匹配一个，就不想下查询
//动态路由访问，访问不到，去访问*
const routes=[
	
	{
	  path:'/',//根路径
	  name:'home',//命名路由
	  components: {
		  default:Index,
		  header:Header,
		  footer:Footer
	  },
		 meta:{
			 keepAlive:true
		 }
	},
	{
	  path:'/login',//根路径
	  name:'login',//命名路由
	  component:()=>import('../views/login.vue')    //懒加载
	},
	{
	  path:'/register',//根路径
	  name:'register',//命名路由
	  component:()=>import('@/views/register.vue')    //懒加载
	},
	{
		path:'/article/:id',  //动态路由 /article/12/other
		component:ArticleDetail,
		children:[
			{
				path:'other', //嵌套路由
				component:ArticleOther
			}
			
		]
	},
	{
		path:'/article', 
		component:ArticleDetail,
		children:[
			{
				path:'other/:id',  ///article/other/12
				component:ArticleOther
			}
			
		]
	},
	{
		path:'/article', //基本路由
		component:()=>import('@/views/article/detail.vue'),
		
	},
	{
		path:'/my',
		name:'my',
		component:()=>import('@/views/my/index.vue')
	},
	{
		path:'/memorabilia',
		name:'memorabilia',
		component:()=>import('@/views/my/memorabilia.vue')
	},
	
	{
		path:'*',//全匹配,要放到最后 
		component:NotFound
	}
	
	
]
const router=new VueRouter({
	mode:'hash', //hash
	base:process.env.BASE_URL,
	routes
})

/* 你可以使用 router.beforeEach 注册一个全局前置守卫：
   创建
   每次访问路由前都要运行全局前置路由守卫
   给我相应的结果
   应用场景：
   授权 用户是否登录
   权限 判断用户是否有权限
 */
router.beforeEach((to,from,next)=>{
	//排除不需要守卫的路径
	let excludeUrl=["/","/login","/register"]
	if(excludeUrl.includes(to.path)){
		console.log(to.path)
		next()
	}
	else{
		let token=store.state.token;
		if(token.length>0){
			//判断权限

			next()
		}
		else{


			next('/login')
		}
		
	}
	
	//如果登录 
	// if(true){
	// 	next()
	// }
	// else{
	// 	next("/login")
	// }
	
	// console.log('beforeEach')
	// console.log('to',to)
	// console.log('from',from)
	// console.log('next',next)
	// next()
})
//全局后置守卫
router.afterEach((to,from)=>{
	
})
export default router;
